#include <allegro.h>
#include <iostream>
#include <cstring>
#include <vector>
#include "util.h"
#include "joypad.h"
#include "cfgreader.h"
#include "tecbattle.hpp"

using std::cout;
using std::vector;
using std::string;

BaseObject::BaseObject(BITMAP *img, int x, int y, int w, int h) {
	this->x=x;
	this->y=y;
	this->w=w;
	this->h=h;
	CFrame=0;
	CDir=0;
	if (img!=NULL) Pic=img;
	if (Pic) CntFrame=Pic->w/w;
}
               
bool BaseObject::Collide(BaseObject* obj) {
	if ((x+(w-w/5))<obj->x || (obj->x+(obj->w-obj->w/5))<x) {
		return false;
	}

	if ((y+w)<obj->y || (obj->y+obj->h)<y) {
		return false;
	}
	return true;
}

Player::Player(BITMAP *img, BITMAP *ups, BITMAP *dns, int x, int y, int w, int h, const char* botdata, int ply):BaseObject(img, x, y, w, h) {
	Cfg=new ConfigReader();
	Cfg->ReadFile(botdata);
	
	Ani=0;
	Anit=0;
	
	rx=x;
	ry=y;
	
	live=true;
	defeated=false;
	
	stp=0;
	stptm=0;
	
	PL=ply;
	
	upst=ups;
	dnst=dns;
	
	slash_a=load_bitmap("bitmaps/slash_atk.tga", NULL);
	blueball_a=load_bitmap("bitmaps/blueball.tga", NULL);
	fire_a=load_bitmap("bitmaps/fire_a.tga", NULL);
	leaf_a=load_bitmap("bitmaps/leaf_a.tga", NULL);
	scare_a=load_bitmap("bitmaps/scare.tga", NULL);
	hit_a=load_bitmap("bitmaps/hit_a_1.tga", NULL);
	fist_a=load_bitmap("bitmaps/fist_a.tga", NULL);
	
	this->Attribute=new int[32];
	Attribute[HP]=Cfg->GetInt("Power");
	Attribute[MAXHP]=Cfg->GetInt("Power");
	Attribute[ATTACK]=Cfg->GetInt("Attack");
	Attribute[DEFENSE]=Cfg->GetInt("Defense");
	Attribute[RANGEATK]=Cfg->GetInt("RangeAttack");
	Attribute[RANGEDEF]=Cfg->GetInt("RangeDefense");
	Attribute[SPEED]=Cfg->GetInt("Speed");
	Attribute[AVOIDANCE]=0;
	Attribute[ATKP]=0;
	Attribute[DEFP]=0;
	Attribute[RATKP]=0;
	Attribute[RDEFP]=0;
	Attribute[SPDP]=0;
	Attribute[TYPE]=Cfg->GetInt("Type");
	Attribute[LV]=Cfg->GetInt("Level");
	
	this->Attacks=new int[8];
	Attacks[1]=Cfg->GetInt("Action1");
	Attacks[2]=Cfg->GetInt("Action2");
	Attacks[3]=Cfg->GetInt("Action3");
	Attacks[4]=Cfg->GetInt("Action4");
	
	this->AP=new int[8];
	AP[1]=0;
	AP[2]=0;
	AP[3]=0;
	AP[4]=0;
	
	if (Cfg->GetInt("Robot")==0) {
		if (ply==1) Pic=load_bitmap("bitmaps/ch1_p1.tga", NULL);
		else if (ply==2) Pic=load_bitmap("bitmaps/ch1_p2.tga", NULL);
	} else if (Cfg->GetInt("Robot")==1) {
		if (ply==1) Pic=load_bitmap("bitmaps/inflatec_p1.tga", NULL);
		else if (ply==2) Pic=load_bitmap("bitmaps/inflatec_p2.tga", NULL);
	} else if (Cfg->GetInt("Robot")==2) {
		if (ply==1) Pic=load_bitmap("bitmaps/listectrix_p1.tga", NULL);
		else if (ply==2) Pic=load_bitmap("bitmaps/listectrix_p2.tga", NULL);
	} else if (Cfg->GetInt("Robot")==3) {
		if (ply==1) Pic=load_bitmap("bitmaps/hydrotec_p1.tga", NULL);
		else if (ply==2) Pic=load_bitmap("bitmaps/hydrotec_p2.tga", NULL);
	}
	
	callname=new char[16];
	Cfg->GetString("Name", callname);
	
	/* Animation ANI
	* 0 = None
	* 1 = Hits
	* 2 = Defeated
	* 3 = Status Up
	* 4 = Status Down
	* 5 = Tackle
	* 6 = Slash
	* 7 = Kick
	* 8 ...
	* ...
	* 
	* 105 = Burn
	* 106 = Par
	*/
}

void Player::STPA() {
	if (stp==1) SetAttribute(HP, GetAttribute(HP)-(GetAttribute(MAXHP)/10));
}

void Player::STPC() {
	if (stp==4) SetAttribute(HP, GetAttribute(HP)-(GetAttribute(MAXHP)/5));
}
		
int Player::GetAttribute(int att) {
	if (att>=2 && att<=6) {
		return Attribute[att]+Attribute[att+6];
	}
	return Attribute[att];
}
		
void Player::WaterP(int x, int y) {
	if (rnd(5)==2) circlefill(doubleBuffer, x, y, 10, makecol(0, rnd(100)+150, rnd(55)+200));
	else rectfill(doubleBuffer, x-10, y-10, x+10, y+10, makecol(0, rnd(100)+150, rnd(55)+200));
}

void Player::ElectroP(int x, int y, int dx, int dy, int dt) {
	int dtx=(dx-x)/dt;
	int dty=(dy-y)/dt;
	int lkx=x;
	int lky=y;
	int olx=x;
	int oly=y;
	
	for (int i=0;i<dt-1;i++) {
		lkx=x+(dtx*(i+1))+(rnd(21)-10);
		lky=y+(dty*(i+1))+(rnd(21)-10);
		line(doubleBuffer, olx, oly, lkx, lky, makecol(rnd(55)+200, rnd(55)+200, 0));
		olx=lkx;
		oly=lky;
	}
	
	line(doubleBuffer, olx, oly, dx, dy, makecol(rnd(55)+200, rnd(55)+200, 0));
}

void Player::SandP(int x, int y, int w, int h) {
	for (int q=0;q<w+(rnd(41)-20);q++) {
		for (int e=0;e<h+(rnd(41)-20);e++) {
			if (rnd(5)==3) putpixel(doubleBuffer, x+q, y+e, makecol(rnd(100)+150,rnd(125)+25,rnd(50)+25));
		}
	}
}

void Player::FlashP(int x, int y) {
	line(doubleBuffer, x, y+25, x, y+75, makecol(250, 250, 250));
	line(doubleBuffer, x+100, y+25, x+100, y+75, makecol(250, 250, 250));
	line(doubleBuffer, x+25, y+32, x+25, y+82, makecol(250, 250, 250));
	line(doubleBuffer, x+32, y, x+32, y+100, makecol(250, 250, 250));
	line(doubleBuffer, x+68, y, x+68, y+100, makecol(250, 250, 250));
	line(doubleBuffer, x+75, y+32, x+75, y+82, makecol(250, 250, 250));
}
		
void Player::Update() {
	if (Ani!=0 && Anit>0) Anit--;
	
	if (stp!=0 && stptm==0) {stp=0;stptm=0;}
	
	if (Ani!=0 && Anit<=0 && Ani!=2) {Ani=0;Anit=0;}
	else if (Ani==2 && Anit<=0) {Ani=0;Anit=0;defeated=true;}
	
	if (Attribute[HP]<=0 && live) {Attribute[HP]=0;live=false;DefeatAni();stp=0;}
	
	if (Attribute[HP]>Attribute[MAXHP]) {Attribute[HP]=Attribute[MAXHP];}
	
	if (!live) Attribute[HP]=0;
}

void Player::Show() {
	if(Pic && !defeated) {
		if (Ani==0) {masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);}
		else if (Ani==1) {
			if (Anit>40) ;
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>20) ;
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else ;
		}
		else if (Ani==2) {
			y=ry+(120-(Anit*3));
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h-(120-(Anit*3)));
		}
		else if (Ani==3) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>65) masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>60) masked_blit(upst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>55) masked_blit(upst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>50) masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>45) masked_blit(upst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>40) masked_blit(upst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>35) masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>30) masked_blit(upst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>25) masked_blit(upst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>20) masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>15) masked_blit(upst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>10) masked_blit(upst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>5) masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else masked_blit(upst, doubleBuffer, 0, 0, x+w, y, 40, 240);
		} else if (Ani==4) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>65) masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>60) masked_blit(dnst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>55) masked_blit(dnst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>50) masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>45) masked_blit(dnst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>40) masked_blit(dnst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>35) masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>30) masked_blit(dnst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>25) masked_blit(dnst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>20) masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else if (Anit>15) masked_blit(dnst, doubleBuffer, 40, 0, x+w, y, 40, 240);
			else if (Anit>10) masked_blit(dnst, doubleBuffer, 80, 0, x+w, y, 40, 240);
			else if (Anit>5) masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
			else masked_blit(dnst, doubleBuffer, 0, 0, x+w, y, 40, 240);
		}
		else if (Ani==105 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit==45 || Anit==40 || Anit==35 || Anit==30 || Anit==25 || Anit==20 || Anit==15) {}
			else if (Anit<=50) {
				stretch_sprite(doubleBuffer, fire_a, x+40+(rnd(7)-3), y+70+(rnd(7)-3), 50, 50);
				stretch_sprite(doubleBuffer, fire_a, x+120+(rnd(7)-3), y+10+(rnd(7)-3), 40, 40);
				stretch_sprite(doubleBuffer, fire_a, x+30+(rnd(7)-3), y+30+(rnd(7)-3), 50, 50);
				stretch_sprite(doubleBuffer, fire_a, x+100+(rnd(7)-3), y+50+(rnd(7)-3), 60, 60);
			}
		} else if (Ani==105 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit==45 || Anit==40 || Anit==35 || Anit==30 || Anit==25 || Anit==20 || Anit==15) {}
			else if (Anit<=50) {
				stretch_sprite(doubleBuffer, fire_a, x+20+(rnd(7)-3), y+70+(rnd(7)-3), 30, 30);
				stretch_sprite(doubleBuffer, fire_a, x+100+(rnd(7)-3), y+10+(rnd(7)-3), 20, 20);
				stretch_sprite(doubleBuffer, fire_a, x+10+(rnd(7)-3), y+30+(rnd(7)-3), 30, 30);
				stretch_sprite(doubleBuffer, fire_a, x+80+(rnd(7)-3), y+50+(rnd(7)-3), 40, 40);
			}
		}
		else if (Ani==106 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=50) {
				ElectroP(460+(rnd(41)-20), 140-20+(rnd(41)-20), 460+(rnd(41)-20), 140-70+(rnd(41)-20));
				ElectroP(460+(rnd(41)-20), 140+20+(rnd(41)-20), 460+(rnd(41)-20), 140+70+(rnd(41)-20));
				ElectroP(460+30+(rnd(41)-20), 140+(rnd(41)-20), 460+30+(rnd(41)-20), 140-40+(rnd(41)-20));
				ElectroP(460+30+(rnd(41)-20), 140+(rnd(41)-20), 460+30+(rnd(41)-20), 140+40+(rnd(41)-20));
				ElectroP(460-30+(rnd(41)-20), 140+(rnd(41)-20), 460-30+(rnd(41)-20), 140-40+(rnd(41)-20));
				ElectroP(460-30+(rnd(41)-20), 140+(rnd(41)-20), 460-30+(rnd(41)-20), 140+40+(rnd(41)-20));
			}
		} else if (Ani==106 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=50) {
				ElectroP(150+(rnd(41)-20), 300-20+(rnd(41)-20), 150+(rnd(41)-20), 300-70+(rnd(41)-20));
				ElectroP(150+(rnd(41)-20), 300+20+(rnd(41)-20), 150+(rnd(41)-20), 300+70+(rnd(41)-20));
				ElectroP(150+50+(rnd(41)-20), 300+(rnd(41)-20), 150+50+(rnd(41)-20), 300-60+(rnd(41)-20));
				ElectroP(150+50+(rnd(41)-20), 300+(rnd(41)-20), 150+50+(rnd(41)-20), 300+60+(rnd(41)-20));
				ElectroP(150-50+(rnd(41)-20), 300+(rnd(41)-20), 150-50+(rnd(41)-20), 300-60+(rnd(41)-20));
				ElectroP(150-50+(rnd(41)-20), 300+(rnd(41)-20), 150-50+(rnd(41)-20), 300+60+(rnd(41)-20));
			}
		}
		
		else if (Ani==5 && PL==1) {
			if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>5) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+(20-Anit), y-(20-Anit), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+Anit, y-Anit, w, h);
		} else if (Ani==5 && PL==2) {
			if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>5) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-(20-Anit), y+(20-Anit), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-Anit, y+Anit, w, h);
		}
		else if (Ani==6 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>20) ;
			else if (Anit>3) masked_blit(slash_a, doubleBuffer, Anit*6, 0, 400+(Anit*6), 80, w-(Anit*6), h-(Anit*6));
			else masked_blit(slash_a, doubleBuffer, 0, 0, 400, 80, w, h);
		} else if (Ani==6 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>20) ;
			else if (Anit>3) masked_blit(slash_a, doubleBuffer, Anit*10, 0, 80+(Anit*10), 220, 200-(Anit*10), 200-(Anit*10));
			else masked_blit(slash_a, doubleBuffer, 0, 0, 80, 220, 200, 200);
		}
		else if (Ani==7 && PL==1) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>35) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+(50-Anit), y-(50-Anit), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+Anit, y-Anit, w, h);
			if (Anit>40) ;
			else if (Anit>10) circlefill(doubleBuffer, 460, 140, 30, makecol(0, 0, 0));
		} else if (Ani==7 && PL==2) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>35) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-(50-Anit), y+(50-Anit), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-Anit, y+Anit, w, h);
			if (Anit>40) ;
			else if (Anit>10) circlefill(doubleBuffer, 110, 280, 30, makecol(0, 0, 0));
		}
		else if (Ani==8 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10) stretch_sprite(doubleBuffer, blueball_a, x+((Anit-10)*2), y+((Anit-10)*2), (60-Anit)*4, (60-Anit)*4);
			else stretch_sprite(doubleBuffer, blueball_a, x, y, 200, 200);
		} else if (Ani==8 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10) stretch_sprite(doubleBuffer, blueball_a, x+10+(Anit-10), y+10+(Anit-10), (60-Anit)*2, (60-Anit)*2);
			else draw_sprite(doubleBuffer, blueball_a, x+10, y+10);
		}
		else if (Ani==9 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit==45 || Anit==40 || Anit==35 || Anit==30 || Anit==25 || Anit==20 || Anit==15) ;
			else if (Anit<=50) draw_sprite(doubleBuffer, fire_a, 420+(rnd(7)-3), 100+(rnd(7)-3));
		} else if (Ani==9 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit==45 || Anit==40 || Anit==35 || Anit==30 || Anit==25 || Anit==20 || Anit==15) ;
			else if (Anit<=50) stretch_sprite(doubleBuffer, fire_a, 100+(rnd(7)-3), 260+(rnd(7)-3), 100, 100);
		}
		else if (Ani==10 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>50) WaterP((x+150)+(25*(60-Anit)), (y+50)-(15*(60-Anit)));
			if (Anit>45 && Anit<=55) WaterP((x+150)+(24*(55-Anit)), (y+50)-(14*(55-Anit)));
			if (Anit>40 && Anit<=50) WaterP((x+150)+(25*(50-Anit)), (y+50)-(13*(50-Anit)));
			if (Anit>35 && Anit<=45) WaterP((x+150)+(26*(45-Anit)), (y+50)-(14*(45-Anit)));
			if (Anit>30 && Anit<=40) WaterP((x+150)+(25*(40-Anit)), (y+50)-(15*(40-Anit)));
			if (Anit>25 && Anit<=35) WaterP((x+150)+(24*(35-Anit)), (y+50)-(14*(35-Anit)));
			if (Anit>20 && Anit<=30) WaterP((x+150)+(25*(30-Anit)), (y+50)-(13*(30-Anit)));
			if (Anit>15 && Anit<=25) WaterP((x+150)+(26*(25-Anit)), (y+50)-(14*(25-Anit)));
			if (Anit>10 && Anit<=20) WaterP((x+150)+(25*(20-Anit)), (y+50)-(15*(20-Anit)));
			
			if (Anit<=50 && Anit>5) {
				WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);
				WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);
				WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);WaterP(rnd(50)+405, rnd(50)+125);
			}
		} else if (Ani==10 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>50) WaterP((x+30)-(25*(60-Anit)), (y+70)+(15*(60-Anit)));
			if (Anit>45 && Anit<=55) WaterP((x+30)-(24*(55-Anit)), (y+70)+(14*(55-Anit)));
			if (Anit>40 && Anit<=50) WaterP((x+30)-(25*(50-Anit)), (y+70)+(13*(50-Anit)));
			if (Anit>35 && Anit<=45) WaterP((x+30)-(26*(45-Anit)), (y+70)+(14*(45-Anit)));
			if (Anit>30 && Anit<=40) WaterP((x+30)-(25*(40-Anit)), (y+70)+(15*(40-Anit)));
			if (Anit>25 && Anit<=35) WaterP((x+30)-(24*(35-Anit)), (y+70)+(14*(35-Anit)));
			if (Anit>20 && Anit<=30) WaterP((x+30)-(25*(30-Anit)), (y+70)+(13*(30-Anit)));
			if (Anit>15 && Anit<=25) WaterP((x+30)-(26*(25-Anit)), (y+70)+(14*(25-Anit)));
			if (Anit>10 && Anit<=20) WaterP((x+30)-(25*(20-Anit)), (y+70)+(15*(20-Anit)));
			
			if (Anit<=50 && Anit>5) {
				WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);
				WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);
				WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);WaterP(rnd(50)+205, rnd(50)+245);
			}
		}
		else if (Ani==11 && PL==1) {
			if (Anit>50) draw_sprite(doubleBuffer, leaf_a, x+180, y-50);
			if (Anit>40) draw_sprite(doubleBuffer, leaf_a, x+100, y-30);
			if (Anit>30) draw_sprite(doubleBuffer, leaf_a, x+200, y+80);
			if (Anit>20) draw_sprite(doubleBuffer, leaf_a, x+120, y);
			if (Anit>10) draw_sprite(doubleBuffer, leaf_a, x, y+50);
			
			if (Anit>40 && Anit<=50) draw_sprite(doubleBuffer, leaf_a, (x+180)+(25*(50-Anit)), (y-50)-(15*(50-Anit)));
			if (Anit>30 && Anit<=40) draw_sprite(doubleBuffer, leaf_a, (x+100)+(25*(40-Anit)), (y-30)-(15*(40-Anit)));
			if (Anit>20 && Anit<=30) draw_sprite(doubleBuffer, leaf_a, (x+200)+(25*(30-Anit)), (y+80)-(15*(30-Anit)));
			if (Anit>10 && Anit<=20) draw_sprite(doubleBuffer, leaf_a, (x+120)+(25*(20-Anit)), y-(15*(20-Anit)));
			if (Anit<=10) draw_sprite(doubleBuffer, leaf_a, x+(25*(10-Anit)), (y+50)-(15*(10-Anit)));
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		} else if (Ani==11 && PL==2) {
			if (Anit>50) rotate_sprite(doubleBuffer, leaf_a, x+180, y-50, itofix(140));
			if (Anit>40) rotate_sprite(doubleBuffer, leaf_a, x+100, y-30, itofix(140));
			if (Anit>30) rotate_sprite(doubleBuffer, leaf_a, x+200, y+80, itofix(140));
			if (Anit>20) rotate_sprite(doubleBuffer, leaf_a, x+120, y, itofix(140));
			if (Anit>10) rotate_sprite(doubleBuffer, leaf_a, x, y+50, itofix(140));
			
			if (Anit>40 && Anit<=50) rotate_sprite(doubleBuffer, leaf_a, (x+180)-(25*(50-Anit)), (y-50)+(15*(50-Anit)), itofix(140));
			if (Anit>30 && Anit<=40) rotate_sprite(doubleBuffer, leaf_a, (x+100)-(25*(40-Anit)), (y-30)+(15*(40-Anit)), itofix(140));
			if (Anit>20 && Anit<=30) rotate_sprite(doubleBuffer, leaf_a, (x+200)-(25*(30-Anit)), (y+80)+(15*(30-Anit)), itofix(140));
			if (Anit>10 && Anit<=20) rotate_sprite(doubleBuffer, leaf_a, (x+120)-(25*(20-Anit)), y+(15*(20-Anit)), itofix(140));
			if (Anit<=10) rotate_sprite(doubleBuffer, leaf_a, x-(25*(10-Anit)), (y+50)+(15*(10-Anit)), itofix(140));
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		}
		else if (Ani==12 && PL==1) {
			if (Anit>50) {}
			else if (Anit>30) {set_trans_blender(0, 0, 0, 200);draw_trans_sprite(doubleBuffer, scare_a, x+130, y-30);}
			else if (Anit>10) {stretch_sprite(doubleBuffer, scare_a, x+130+(30-Anit), y-30-((30-Anit)*2), 156+((30-Anit)*2), 100+((30-Anit)*2));}
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		} else if (Ani==12 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>50) {}
			else if (Anit>30) {set_trans_blender(0, 0, 0, 200);draw_trans_sprite(doubleBuffer, scare_a, x-50, y+30);}
			else if (Anit>10) {stretch_sprite(doubleBuffer, scare_a, x-50-((30-Anit)*2), y+30+(30-Anit), 156+((30-Anit)*2), 100+((30-Anit)*2));}
		}
		else if (Ani==13 && PL==1) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((60-Anit)*4), y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+40-((50-Anit)*4), y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-40+((30-Anit)*4), y, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+40-((10-Anit)*4), y, w, h);
		} else if (Ani==13 && PL==2) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((60-Anit)*3), y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30-((50-Anit)*3), y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-30+((30-Anit)*3), y, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30-((10-Anit)*3), y, w, h);
		}
		else if (Ani==14 && PL==1) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((60-Anit)*3), y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30-((50-Anit)*3), y, w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-30+((30-Anit)*3), y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((20-Anit)*2), y-((20-Anit)*2), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+20-((10-Anit)*2), y-20+((10-Anit)*2), w, h);
		} else if (Ani==14 && PL==2) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((60-Anit)*2), y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+20-((50-Anit)*2), y, w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-20+((30-Anit)*2), y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-(20-Anit), y+(20-Anit), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-10+((10-Anit)*1), y+10-(10-Anit), w, h);
		}
		else if (Ani==15 && PL==1) {
			if (Anit>45 && Anit<=50) {draw_sprite(doubleBuffer, hit_a, 460, 110);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+10, y-10, w, h);}
			else if (Anit>35 && Anit<=40) {draw_sprite(doubleBuffer, hit_a, 420, 140);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+10, y-10, w, h);}
			else if (Anit>25 && Anit<=30) {draw_sprite(doubleBuffer, hit_a, 420, 110);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+10, y-10, w, h);}
			else if (Anit>15 && Anit<=20) {draw_sprite(doubleBuffer, hit_a, 460, 140);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+10, y-10, w, h);}
			else {masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);}
		} else if (Ani==15 && PL==2) {
			if (Anit>45 && Anit<=50) {draw_sprite(doubleBuffer, hit_a, 160, 260);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-10, y+10, w, h);}
			else if (Anit>35 && Anit<=40) {draw_sprite(doubleBuffer, hit_a, 100, 310);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-10, y+10, w, h);}
			else if (Anit>25 && Anit<=30) {draw_sprite(doubleBuffer, hit_a, 100, 260);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-10, y+10, w, h);}
			else if (Anit>15 && Anit<=20) {draw_sprite(doubleBuffer, hit_a, 160, 310);masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-10, y+10, w, h);}
			else {masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);}
		}
		else if (Ani==16 && PL==1) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-((60-Anit)*3), y+((60-Anit)*2), w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-30, y+20, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-30+((20-Anit)*6), y+20-((20-Anit)*4), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30-((10-Anit)*3), y-20+((10-Anit)*2), w, h);
		} else if (Ani==16 && PL==2) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+((60-Anit)*3), y-((60-Anit)*2), w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30, y-20, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+30-((20-Anit)*6), y-20+((20-Anit)*4), w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-30+((10-Anit)*3), y+20-((10-Anit)*2), w, h);
		}
		else if (Ani==17 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=50) {
				ElectroP(460+(rnd(41)-20), 140-20+(rnd(41)-20), 460+(rnd(41)-20), 140-70+(rnd(41)-20));
				ElectroP(460+(rnd(41)-20), 140+20+(rnd(41)-20), 460+(rnd(41)-20), 140+70+(rnd(41)-20));
				ElectroP(460+30+(rnd(41)-20), 140+(rnd(41)-20), 460+30+(rnd(41)-20), 140-40+(rnd(41)-20));
				ElectroP(460+30+(rnd(41)-20), 140+(rnd(41)-20), 460+30+(rnd(41)-20), 140+40+(rnd(41)-20));
				ElectroP(460-30+(rnd(41)-20), 140+(rnd(41)-20), 460-30+(rnd(41)-20), 140-40+(rnd(41)-20));
				ElectroP(460-30+(rnd(41)-20), 140+(rnd(41)-20), 460-30+(rnd(41)-20), 140+40+(rnd(41)-20));
			}
		} else if (Ani==17 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=50) {
				ElectroP(150+(rnd(41)-20), 300-20+(rnd(41)-20), 150+(rnd(41)-20), 300-70+(rnd(41)-20));
				ElectroP(150+(rnd(41)-20), 300+20+(rnd(41)-20), 150+(rnd(41)-20), 300+70+(rnd(41)-20));
				ElectroP(150+50+(rnd(41)-20), 300+(rnd(41)-20), 150+50+(rnd(41)-20), 300-60+(rnd(41)-20));
				ElectroP(150+50+(rnd(41)-20), 300+(rnd(41)-20), 150+50+(rnd(41)-20), 300+60+(rnd(41)-20));
				ElectroP(150-50+(rnd(41)-20), 300+(rnd(41)-20), 150-50+(rnd(41)-20), 300-60+(rnd(41)-20));
				ElectroP(150-50+(rnd(41)-20), 300+(rnd(41)-20), 150-50+(rnd(41)-20), 300+60+(rnd(41)-20));
			}
		}
		else if (Ani==18 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit<=50) {
				line(doubleBuffer, -100+(50-Anit)*35, 90, (50-Anit)*35, 90, makecol(235, 235, 235));
				line(doubleBuffer, -100+(45-Anit)*35, 130, (45-Anit)*35, 130, makecol(235, 235, 235));
				line(doubleBuffer, -100+(40-Anit)*35, 110, (40-Anit)*35, 110, makecol(235, 235, 235));
				line(doubleBuffer, -100+(35-Anit)*35, 200, (35-Anit)*35, 200, makecol(235, 235, 235));
				line(doubleBuffer, -100+(30-Anit)*35, 170, (30-Anit)*35, 170, makecol(235, 235, 235));
				line(doubleBuffer, -100+(25-Anit)*35, 230, (25-Anit)*35, 230, makecol(235, 235, 235));
				line(doubleBuffer, -100+(20-Anit)*35, 120, (20-Anit)*35, 120, makecol(235, 235, 235));
				line(doubleBuffer, -100+(15-Anit)*35, 150, (15-Anit)*35, 150, makecol(235, 235, 235));
				
				line(doubleBuffer, -150+(47-Anit)*40, 150, (47-Anit)*40, 150, makecol(235, 235, 235));
				line(doubleBuffer, -150+(42-Anit)*40, 120, (42-Anit)*40, 120, makecol(235, 235, 235));
				line(doubleBuffer, -150+(37-Anit)*40, 230, (37-Anit)*40, 230, makecol(235, 235, 235));
				line(doubleBuffer, -150+(32-Anit)*40, 170, (32-Anit)*40, 170, makecol(235, 235, 235));
				line(doubleBuffer, -150+(27-Anit)*40, 200, (27-Anit)*40, 200, makecol(235, 235, 235));
				line(doubleBuffer, -150+(22-Anit)*40, 110, (22-Anit)*40, 110, makecol(235, 235, 235));
				line(doubleBuffer, -150+(17-Anit)*40, 130, (17-Anit)*40, 130, makecol(235, 235, 235));
				line(doubleBuffer, -150+(12-Anit)*40, 90, (12-Anit)*40, 90, makecol(235, 235, 235));
			}
		} else if (Ani==18 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit<=50) {
				line(doubleBuffer, 740-(50-Anit)*35, 280, 640-(50-Anit)*35, 280, makecol(235, 235, 235));
				line(doubleBuffer, 740-(45-Anit)*35, 290, 640-(45-Anit)*35, 290, makecol(235, 235, 235));
				line(doubleBuffer, 740-(40-Anit)*35, 240, 640-(40-Anit)*35, 240, makecol(235, 235, 235));
				line(doubleBuffer, 740-(35-Anit)*35, 270, 640-(35-Anit)*35, 270, makecol(235, 235, 235));
				line(doubleBuffer, 740-(30-Anit)*35, 225, 640-(30-Anit)*35, 225, makecol(235, 235, 235));
				line(doubleBuffer, 740-(25-Anit)*35, 300, 640-(25-Anit)*35, 300, makecol(235, 235, 235));
				line(doubleBuffer, 740-(20-Anit)*35, 250, 640-(20-Anit)*35, 250, makecol(235, 235, 235));
				line(doubleBuffer, 740-(15-Anit)*35, 230, 640-(15-Anit)*35, 230, makecol(235, 235, 235));
				
				line(doubleBuffer, 790-(47-Anit)*40, 230, 640-(47-Anit)*40, 230, makecol(235, 235, 235));
				line(doubleBuffer, 790-(42-Anit)*40, 250, 640-(42-Anit)*40, 250, makecol(235, 235, 235));
				line(doubleBuffer, 790-(37-Anit)*40, 300, 640-(37-Anit)*40, 300, makecol(235, 235, 235));
				line(doubleBuffer, 790-(32-Anit)*40, 225, 640-(32-Anit)*40, 225, makecol(235, 235, 235));
				line(doubleBuffer, 790-(27-Anit)*40, 270, 640-(27-Anit)*40, 270, makecol(235, 235, 235));
				line(doubleBuffer, 790-(22-Anit)*40, 240, 640-(22-Anit)*40, 240, makecol(235, 235, 235));
				line(doubleBuffer, 790-(17-Anit)*40, 290, 640-(17-Anit)*40, 290, makecol(235, 235, 235));
				line(doubleBuffer, 790-(12-Anit)*40, 280, 640-(12-Anit)*40, 280, makecol(235, 235, 235));
			}
		}
		else if (Ani==19 && PL==1) {
			if (Anit>40) {}
			else if (Anit>20) {SandP((x+150)+(13*(40-Anit)), (y+40)-(8*(40-Anit)), 80, 80);}
			if (Anit>5 && Anit<=20) SandP(410, 90, 100, 100);
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		} else if (Ani==19 && PL==2) {
			if (Anit>40) {}
			else if (Anit>20) {SandP((x+30)-(13*(40-Anit)), (y+40)+(8*(40-Anit)), 80, 80);}
			if (Anit>5 && Anit<=20) SandP(75, 245, 150, 150);
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		}
		else if (Ani==20 && PL==1) {
			if (Anit>40 && Anit<=50) circlefill(doubleBuffer, (x+150)+(25*(50-Anit)), (y+40)-(15*(50-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>35 && Anit<=45) circlefill(doubleBuffer, (x+150)+(25*(45-Anit)), (y+40)-(15*(45-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>30 && Anit<=40) circlefill(doubleBuffer, (x+150)+(25*(40-Anit)), (y+40)-(15*(40-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>25 && Anit<=35) circlefill(doubleBuffer, (x+150)+(25*(35-Anit)), (y+40)-(15*(35-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>20 && Anit<=30) circlefill(doubleBuffer, (x+150)+(25*(30-Anit)), (y+40)-(15*(30-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>15 && Anit<=25) circlefill(doubleBuffer, (x+150)+(25*(25-Anit)), (y+40)-(15*(25-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>10 && Anit<=20) circlefill(doubleBuffer, (x+150)+(25*(20-Anit)), (y+40)-(15*(20-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		} else if (Ani==20 && PL==2) {
			if (Anit>40 && Anit<=50) circlefill(doubleBuffer, (x+30)-(25*(50-Anit)), (y+40)+(15*(50-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>35 && Anit<=45) circlefill(doubleBuffer, (x+30)-(25*(45-Anit)), (y+40)+(15*(45-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>30 && Anit<=40) circlefill(doubleBuffer, (x+30)-(25*(40-Anit)), (y+40)+(15*(40-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>25 && Anit<=35) circlefill(doubleBuffer, (x+30)-(25*(35-Anit)), (y+40)+(15*(35-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>20 && Anit<=30) circlefill(doubleBuffer, (x+30)-(25*(30-Anit)), (y+40)+(15*(30-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>15 && Anit<=25) circlefill(doubleBuffer, (x+30)-(25*(25-Anit)), (y+40)+(15*(25-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			if (Anit>10 && Anit<=20) circlefill(doubleBuffer, (x+30)-(25*(20-Anit)), (y+40)+(15*(20-Anit)), 30, makecol(rnd(100)+150, rnd(50), rnd(100)+150));
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		}
		else if (Ani==21 && PL==1) {
			if (Anit>20 && Anit<=32) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+20, y-20, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=30) draw_sprite(doubleBuffer, fist_a, 430, 120);
		} else if (Ani==21 && PL==2) {
			if (Anit>20 && Anit<=32) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-20, y+20, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>10 && Anit<=30) draw_sprite(doubleBuffer, fist_a, 120, 270);
		}
		else if (Ani==22 && PL==1) {
			if (Anit>40) {
				circle(doubleBuffer, x+170, y+32, (Anit-40)*10, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, ((Anit-40)*10)-1, makecol(50, 150, 250));
				circle(doubleBuffer, x+170, y+32, ((Anit-40)*10)-2, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, ((Anit-40)*10)-3, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, ((Anit-40)*10)-4, makecol(50, 150, 250));
				circle(doubleBuffer, x+170, y+32, ((Anit-40)*10)-5, makecol(30, 120, 200));
			} else if (Anit>30) {
				circle(doubleBuffer, x+170, y+32, 1, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, 2, makecol(50, 150, 250));
				circle(doubleBuffer, x+170, y+32, 3, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, 4, makecol(30, 120, 200));
				circle(doubleBuffer, x+170, y+32, 5, makecol(50, 150, 250));
				circle(doubleBuffer, x+170, y+32, 6, makecol(30, 120, 200));
			} else {
				line(doubleBuffer, x+170, y+32, 460, 140, makecol(30, 120, 200));
				line(doubleBuffer, x+170, y+32, 461, 141, makecol(50, 140, 220));
				line(doubleBuffer, x+170, y+32, 462, 142, makecol(70, 160, 240));
				line(doubleBuffer, x+170, y+32, 459, 139, makecol(75, 200, 255));
				line(doubleBuffer, x+170, y+32, 458, 138, makecol(30, 120, 200));
				line(doubleBuffer, x+170, y+32, 463, 143, makecol(50, 140, 220));
				line(doubleBuffer, x+170, y+32, 457, 137, makecol(70, 160, 240));
			}
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
		} else if (Ani==22 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>40) {
				circle(doubleBuffer, x+60, y+60, (Anit-40)*10, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, ((Anit-40)*10)-1, makecol(50, 150, 250));
				circle(doubleBuffer, x+60, y+60, ((Anit-40)*10)-2, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, ((Anit-40)*10)-3, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, ((Anit-40)*10)-4, makecol(50, 150, 250));
				circle(doubleBuffer, x+60, y+60, ((Anit-40)*10)-5, makecol(30, 120, 200));
			} else if (Anit>30) {
				circle(doubleBuffer, x+60, y+60, 1, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, 2, makecol(50, 150, 250));
				circle(doubleBuffer, x+60, y+60, 3, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, 4, makecol(30, 120, 200));
				circle(doubleBuffer, x+60, y+60, 5, makecol(50, 150, 250));
				circle(doubleBuffer, x+60, y+60, 6, makecol(30, 120, 200));
			} else {
				line(doubleBuffer, x+60, y+60, 215, 255, makecol(30, 120, 200));
				line(doubleBuffer, x+60, y+60, 216, 256, makecol(50, 140, 220));
				line(doubleBuffer, x+60, y+60, 217, 257, makecol(70, 160, 240));
				line(doubleBuffer, x+60, y+60, 214, 254, makecol(75, 200, 255));
				line(doubleBuffer, x+60, y+60, 213, 253, makecol(30, 120, 200));
				line(doubleBuffer, x+60, y+60, 218, 258, makecol(50, 140, 220));
				line(doubleBuffer, x+60, y+60, 212, 252, makecol(70, 160, 240));
			}
		}
		else if (Ani==23 && PL==1) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>40) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+100, y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-100, y, w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+100, y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-100, y, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			
			if (Anit>48 && Anit<=50) FlashP(x+50, y+25);
			if (Anit>44 && Anit<=46) FlashP(x+50, y+25);
			if (Anit>40 && Anit<=42) FlashP(x+50, y+25);
			
			if (Anit>38 && Anit<=40) FlashP(x+150, y+25);
			if (Anit>34 && Anit<=36) FlashP(x+150, y+25);
			if (Anit>30 && Anit<=32) FlashP(x+150, y+25);
			
			if (Anit>28 && Anit<=30) FlashP(x-100, y+25);
			if (Anit>24 && Anit<=26) FlashP(x-100, y+25);
			if (Anit>20 && Anit<=22) FlashP(x-100, y+25);
			
			if (Anit>18 && Anit<=20) FlashP(x+150, y+25);
			if (Anit>14 && Anit<=16) FlashP(x+150, y+25);
			if (Anit>10 && Anit<=12) FlashP(x+150, y+25);
			
			if (Anit>18 && Anit<=20) FlashP(x-100, y+25);
			if (Anit>14 && Anit<=16) FlashP(x-100, y+25);
		} else if (Ani==23 && PL==2) {
			if (Anit>50) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			else if (Anit>40) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+70, y, w, h);
			else if (Anit>30) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-70, y, w, h);
			else if (Anit>20) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x+70, y, w, h);
			else if (Anit>10) masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x-70, y, w, h);
			else masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			
			if (Anit>48 && Anit<=50) FlashP(x+10, y+25);
			if (Anit>44 && Anit<=46) FlashP(x+10, y+25);
			if (Anit>40 && Anit<=42) FlashP(x+10, y+25);
			
			if (Anit>38 && Anit<=40) FlashP(x+80, y+25);
			if (Anit>34 && Anit<=36) FlashP(x+80, y+25);
			if (Anit>30 && Anit<=32) FlashP(x+80, y+25);
			
			if (Anit>28 && Anit<=30) FlashP(x-60, y+25);
			if (Anit>24 && Anit<=26) FlashP(x-60, y+25);
			if (Anit>20 && Anit<=22) FlashP(x-60, y+25);
			
			if (Anit>18 && Anit<=20) FlashP(x+80, y+25);
			if (Anit>14 && Anit<=16) FlashP(x+80, y+25);
			if (Anit>10 && Anit<=12) FlashP(x+80, y+25);
			
			if (Anit>18 && Anit<=20) FlashP(x-60, y+25);
			if (Anit>14 && Anit<=16) FlashP(x-60, y+25);
		}
		else if (Ani==24 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			
			if (Anit>50) {
				line(doubleBuffer, 458, 80, 458, 80+((60-Anit)*12), makecol(50, 100, 200));
				line(doubleBuffer, 459, 80, 459, 80+((60-Anit)*12), makecol(75, 150, 230));
				line(doubleBuffer, 460, 80, 460, 80+((60-Anit)*12), makecol(100, 200, 255));
				line(doubleBuffer, 461, 80, 461, 80+((60-Anit)*12), makecol(75, 150, 230));
				line(doubleBuffer, 462, 80, 462, 80+((60-Anit)*12), makecol(50, 100, 200));
			} else if (Anit>40) {
				line(doubleBuffer, 458, 80+((50-Anit)*12), 458, 200, makecol(50, 100, 200));
				line(doubleBuffer, 459, 80+((50-Anit)*12), 459, 200, makecol(75, 150, 230));
				line(doubleBuffer, 460, 80+((50-Anit)*12), 460, 200, makecol(100, 200, 255));
				line(doubleBuffer, 461, 80+((50-Anit)*12), 461, 200, makecol(75, 150, 230));
				line(doubleBuffer, 462, 80+((50-Anit)*12), 462, 200, makecol(50, 100, 200));
			} else if (Anit>30) {
			} else if (Anit>20) {
				line(doubleBuffer, 458, 80, 458, 200, makecol(50, 100, 200));
				line(doubleBuffer, 459, 80, 459, 200, makecol(75, 150, 230));
				line(doubleBuffer, 461, 80, 461, 200, makecol(75, 150, 230));
				line(doubleBuffer, 462, 80, 462, 200, makecol(50, 100, 200));
			} else {
				line(doubleBuffer, 458-((20-Anit)*3), 80, 458-((20-Anit)*3), 200, makecol(50, 100, 200));
				line(doubleBuffer, 459-((20-Anit)*3), 80, 459-((20-Anit)*3), 200, makecol(75, 150, 230));
				line(doubleBuffer, 461+((20-Anit)*3), 80, 461+((20-Anit)*3), 200, makecol(75, 150, 230));
				line(doubleBuffer, 462+((20-Anit)*3), 80, 462+((20-Anit)*3), 200, makecol(50, 100, 200));
			}
		} else if (Ani==24 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			
			if (Anit>50) {
				line(doubleBuffer, 148, 220, 148, 220+((60-Anit)*20), makecol(50, 100, 200));
				line(doubleBuffer, 149, 220, 149, 220+((60-Anit)*20), makecol(75, 150, 230));
				line(doubleBuffer, 150, 220, 150, 220+((60-Anit)*20), makecol(100, 200, 255));
				line(doubleBuffer, 151, 220, 151, 220+((60-Anit)*20), makecol(75, 150, 230));
				line(doubleBuffer, 152, 220, 152, 220+((60-Anit)*20), makecol(50, 100, 200));
			} else if (Anit>40) {
				line(doubleBuffer, 148, 220+((50-Anit)*20), 148, 420, makecol(50, 100, 200));
				line(doubleBuffer, 149, 220+((50-Anit)*20), 149, 420, makecol(75, 150, 230));
				line(doubleBuffer, 150, 220+((50-Anit)*20), 150, 420, makecol(100, 200, 255));
				line(doubleBuffer, 151, 220+((50-Anit)*20), 151, 420, makecol(75, 150, 230));
				line(doubleBuffer, 152, 220+((50-Anit)*20), 152, 420, makecol(50, 100, 200));
			} else if (Anit>30) {
			} else if (Anit>20) {
				line(doubleBuffer, 148, 220, 148, 420, makecol(50, 100, 200));
				line(doubleBuffer, 149, 220, 149, 420, makecol(75, 150, 230));
				line(doubleBuffer, 151, 220, 151, 420, makecol(75, 150, 230));
				line(doubleBuffer, 152, 220, 152, 420, makecol(50, 100, 200));
			} else {
				line(doubleBuffer, 148-((20-Anit)*4), 220, 148-((20-Anit)*4), 420, makecol(50, 100, 200));
				line(doubleBuffer, 149-((20-Anit)*4), 220, 149-((20-Anit)*4), 420, makecol(75, 150, 230));
				line(doubleBuffer, 151+((20-Anit)*4), 220, 151+((20-Anit)*4), 420, makecol(75, 150, 230));
				line(doubleBuffer, 152+((20-Anit)*4), 220, 152+((20-Anit)*4), 420, makecol(50, 100, 200));
			}
		}
		else if (Ani==25 && PL==1) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>20) {set_screen_blender(0, 0, 0 , ((60-Anit)*6)+15);draw_trans_sprite(doubleBuffer, Pic, x, y);}
			else {set_screen_blender(0, 0, 0 , (Anit*12)+15);draw_trans_sprite(doubleBuffer, Pic, x, y);}
		} else if (Ani==25 && PL==2) {
			masked_blit(Pic, doubleBuffer, CFrame*w, CDir*h, x, y, w, h);
			if (Anit>20) {set_screen_blender(0, 0, 0 , ((60-Anit)*6)+15);draw_trans_sprite(doubleBuffer, Pic, x, y);}
			else {set_screen_blender(0, 0, 0 , (Anit*12)+15);draw_trans_sprite(doubleBuffer, Pic, x, y);}
		}
	}
}

ActionD::ActionD(BITMAP *img, int x, int y, int w, int h):BaseObject(img, x, y, w, h) {
	phase=0;
	ophase=0;
	firsta=1;
	dly=0;
	atk1=0;
	atk2=0;
	matk1=0;
	matk2=0;
	
	actend=false;
	nnr=false;
	nnr2=false;
	p1rstp=false;
	p2rstp=false;
	
	RW[0][0]=1.0;RW[0][1]=1.0;RW[0][2]=1.0;RW[0][3]=1.0;RW[0][4]=1.0;RW[0][5]=1.0;RW[0][6]=0.5;RW[0][7]=1.0;RW[0][8]=0.5;RW[0][9]=0.5;
	
	RW[1][0]=1.0;RW[1][1]=0.5;RW[1][2]=0.5;RW[1][3]=2.0;RW[1][4]=1.0;RW[1][5]=2.0;RW[1][6]=0.5;RW[1][7]=0.5;RW[1][8]=1.0;RW[1][9]=0.5;
	
	RW[2][0]=1.0;RW[2][1]=2.0;RW[2][2]=0.5;RW[2][3]=0.5;RW[2][4]=1.0;RW[2][5]=1.0;RW[2][6]=2.0;RW[2][7]=0.5;RW[2][8]=1.0;RW[2][9]=1.0;
	
	RW[3][0]=1.0;RW[3][1]=0.5;RW[3][2]=1.0;RW[3][3]=0.5;RW[3][4]=1.0;RW[3][5]=0.5;RW[3][6]=2.0;RW[3][7]=0.5;RW[3][8]=1.0;RW[3][9]=1.0;
	
	RW[4][0]=1.0;RW[4][1]=1.0;RW[4][2]=2.0;RW[4][3]=0.5;RW[4][4]=0.5;RW[4][5]=2.0;RW[4][6]=0.2;RW[4][7]=2.0;RW[4][8]=1.0;RW[4][9]=0.5;
	
	RW[5][0]=1.0;RW[5][1]=1.0;RW[5][2]=1.0;RW[5][3]=2.0;RW[5][4]=1.0;RW[5][5]=1.0;RW[5][6]=1.0;RW[5][7]=2.0;RW[5][8]=2.0;RW[5][9]=1.0;
	
	RW[6][0]=1.0;RW[6][1]=2.0;RW[6][2]=1.0;RW[6][3]=1.0;RW[6][4]=2.0;RW[6][5]=0.2;RW[6][6]=2.0;RW[6][7]=2.0;RW[6][8]=2.0;RW[6][9]=0.5;
	
	RW[7][0]=2.0;RW[7][1]=1.0;RW[7][2]=1.0;RW[7][3]=2.0;RW[7][4]=1.0;RW[7][5]=1.0;RW[7][6]=1.0;RW[7][7]=0.5;RW[7][8]=2.0;RW[7][9]=0.5;
	
	RW[8][0]=2.0;RW[8][1]=1.0;RW[8][2]=1.0;RW[8][3]=0.5;RW[8][4]=1.0;RW[8][5]=0.5;RW[8][6]=2.0;RW[8][7]=0.5;RW[8][8]=1.0;RW[8][9]=2.0;
	
	RW[9][0]=2.0;RW[9][1]=1.0;RW[9][2]=1.0;RW[9][3]=1.0;RW[9][4]=1.0;RW[9][5]=1.0;RW[9][6]=1.0;RW[9][7]=2.0;RW[9][8]=0.5;RW[9][9]=2.0;
	
	
	this->Power=new int[32];
	Power[0]=100;
	Power[1]=90; //high critical hit
	Power[2]=130; //90%
	Power[3]=0; //Defense +
	Power[4]=95; //10% Burn
	Power[5]=100;
	Power[6]=90; //high critical hit
	Power[7]=0; //Enemy Attack -, 20% Shock
	Power[8]=0; //Speed +;
	Power[9]=100; //95%, First Attack
	Power[10]=30; //Attacks 2-5x
	Power[11]=250; //1 round aussetzen
	Power[12]=95; //15% Paralyze
	Power[13]=95; //20% Shocks
	Power[14]=95; //25% Enemy Acc -
	Power[15]=95; //10% Confused
	Power[16]=100;
	Power[17]=110;
	Power[18]=0; //Avoidance +
	Power[19]=0; //Enemy HP:2
	Power[20]=0; //Heal 50%
	
	this->MXAP=new int[32];
	MXAP[0]=30;
	MXAP[1]=20;
	MXAP[2]=20;
	MXAP[3]=30;
	MXAP[4]=20;
	MXAP[5]=20;
	MXAP[6]=20;
	MXAP[7]=20;
	MXAP[8]=20;
	MXAP[9]=15;
	MXAP[10]=15;
	MXAP[11]=5;
	MXAP[12]=20;
	MXAP[13]=15;
	MXAP[14]=30;
	MXAP[15]=20;
	MXAP[16]=20;
	MXAP[17]=15;
	MXAP[18]=15;
	MXAP[19]=10;
	MXAP[20]=10;
	
	this->Acc=new int[32];
	Acc[0]=100;
	Acc[1]=100;
	Acc[2]=90;
	Acc[3]=100;
	Acc[4]=100;
	Acc[5]=100;
	Acc[6]=100;
	Acc[7]=90;
	Acc[8]=100;
	Acc[9]=95;
	Acc[10]=90;
	Acc[11]=80;
	Acc[12]=100;
	Acc[13]=100;
	Acc[14]=95;
	Acc[15]=100;
	Acc[16]=100;
	Acc[17]=100;
	Acc[18]=100;
	Acc[19]=80;
	Acc[20]=100;
	
	this->AT=new int[32];
	AT[0]=0;
	AT[1]=4;
	AT[2]=0;
	AT[3]=1;
	AT[4]=0;
	AT[5]=0;
	AT[6]=4;
	AT[7]=2;
	AT[8]=1;
	AT[9]=3;
	AT[10]=5;
	AT[11]=6;
	AT[12]=0;
	AT[13]=0;
	AT[14]=0;
	AT[15]=0;
	AT[16]=0;
	AT[17]=0;
	AT[18]=1;
	AT[19]=9;
	AT[20]=1;
	
	//AT 0 Normal, 1 Statusus, 2 Statusen, 3 First Atk, 4 High Critical Hit, 5 Combo, 6 no next round, 7 100% Hit, 9 Spezial
	
	this->AK=new int[32];
	AK[0]=0;
	AK[1]=0;
	AK[2]=0;
	AK[3]=2;
	AK[4]=0;
	AK[5]=1;
	AK[6]=0;
	AK[7]=2;
	AK[8]=2;
	AK[9]=0;
	AK[10]=0;
	AK[11]=0;
	AK[12]=1;
	AK[13]=1;
	AK[14]=1;
	AK[15]=1;
	AK[16]=0;
	AK[17]=1;
	AK[18]=2;
	AK[19]=0;
	AK[20]=2;
	
	//AK 0 Close, 1 Ranged, 2 Status
	
	this->STAUS=new int[32];
	STAUS[1]=-1;
	STAUS[2]=-1;
	STAUS[3]=Player::DEFP;
	STAUS[4]=-1;
	STAUS[5]=-1;
	STAUS[6]=-1;
	STAUS[7]=Player::ATKP;
	STAUS[8]=Player::SPDP;
	STAUS[9]=-1;
	STAUS[10]=-1;
	STAUS[11]=-1;
	STAUS[12]=-1;
	STAUS[13]=-1;
	STAUS[14]=-1;
	STAUS[15]=-1;
	STAUS[16]=-1;
	STAUS[17]=-1;
	STAUS[18]=Player::AVOIDANCE;
	STAUS[19]=-1;
	STAUS[20]=Player::HP;
	
	this->STE=new int[32];
	STE[0]=0;
	STE[1]=0;
	STE[2]=0;
	STE[3]=0;
	STE[4]=1;
	STE[5]=0;
	STE[6]=0;
	STE[7]=3;
	STE[8]=0;
	STE[9]=0;
	STE[10]=0;
	STE[11]=0;
	STE[12]=2;
	STE[13]=3;
	STE[14]=0; //25% Enemy Acc-
	STE[15]=4;
	STE[16]=0;
	STE[17]=0;
	STE[18]=0;
	STE[19]=0;
	STE[20]=0;
	
	//%STE 0 None, 1 Burn 10%, 2 Paralyze 15%, 3 Shock 20%, 4 Confuse 10%, 5 Sleep 10%, 6 Uncontrolable 10%
	
	this->AType=new int[32];
	AType[0]=0;
	AType[1]=0;
	AType[2]=0;
	AType[3]=0;
	AType[4]=1;
	AType[5]=2;
	AType[6]=3;
	AType[7]=0;
	AType[8]=0;
	AType[9]=0;
	AType[10]=0;
	AType[11]=0;
	AType[12]=4;
	AType[13]=5;
	AType[14]=6;
	AType[15]=7;
	AType[16]=8;
	AType[17]=9;
	AType[18]=0;
	AType[19]=0;
	AType[20]=0;
	
	//ATYPE 0 Normal, 1 Fire, 2 Water, 3 Grass, 4 Electro, 5 Aero, 6 Ground, 7 Magic, 8 Fight, 9 Astro
	
	AttackNames.push_back("Tackle"); // 0
	AttackNames.push_back("Slash"); // 1
	AttackNames.push_back("Kick"); // 2
	AttackNames.push_back("Guard Shield"); // 3
	AttackNames.push_back("Fire Flare"); // 4
	AttackNames.push_back("Aqua Beam"); // 5
	AttackNames.push_back("Leaf Cutter"); // 6
	AttackNames.push_back("Scare"); // 7
	AttackNames.push_back("Rapidity"); // 8
	AttackNames.push_back("Turbo Strike"); // 9
	AttackNames.push_back("Combo Bash"); // 10
	AttackNames.push_back("Ultra Impact"); // 11
	AttackNames.push_back("Electroshock"); // 12
	AttackNames.push_back("Winddash"); // 13
	AttackNames.push_back("Sandblast"); // 14
	AttackNames.push_back("Magicwave"); // 15
	AttackNames.push_back("Punch"); // 16
	AttackNames.push_back("Astropuls"); // 17
	AttackNames.push_back("Quick Dodge"); // 18
	AttackNames.push_back("Equator Claw"); // 19
	AttackNames.push_back("Regain"); // 20
}

ActionD::~ActionD() {
	delete [] Power;
	delete [] MXAP;
	delete [] Acc;
	delete [] AT;
	delete [] AK;
	delete [] AType;
	delete [] STAUS;
	delete [] STE;
}

int ActionD::NNR() {
	if (nnr && nnr2) return 3;
	else if (nnr) return 1;
	else if (nnr2) return 2;
}

void ActionD::ActionDmg(Player *atk, Player *def, int atkn, int pl) {
	int atkr;
	int defr;
	
	if (AK[atkn]==0) {atkr=Player::ATTACK;defr=Player::DEFENSE;}
	else if (AK[atkn]==1) {atkr=Player::RANGEATK;defr=Player::RANGEDEF;}
	
	if (AT[atkn]==0 || AT[atkn]==3 || AT[atkn]==6) {
		if (rnd(100)<=Acc[atkn]-def->GetAttribute(Player::AVOIDANCE)) {
			int AW=atk->GetAttribute(atkr)-def->GetAttribute(defr);
			int DMG=(int) ((float) AW*((float) Power[atkn]/100.0));
			if (DMG<=1) DMG=2;
			DMG=(int) ((float) DMG*(float) RW[AType[atkn]][def->GetType()]);
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
		
			def->SetAttribute(Player::HP, def->GetAttribute(Player::HP)-DMG);
			def->DmgAni();
			
			if (AT[atkn]==6 && atk->GetPL()==1) nnr=true;
			else if (AT[atkn]==6 && atk->GetPL()==2) nnr2=true;
		} else {
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			ophase=phase;
			phase=8;
			dly=90;
		}
	} else if (AT[atkn]==4) {
		if (rnd(100)<=Acc[atkn]-def->GetAttribute(Player::AVOIDANCE)) {
			int AW=atk->GetAttribute(atkr)-def->GetAttribute(defr);
			int DMG=(int) ((float) AW*((float) Power[atkn]/100.0));
			if (DMG<=1) DMG=2;
			DMG=(int) ((double) DMG*RW[AType[atkn]][def->GetType()]);
			if (rnd(100)<=30) DMG*=2;
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
		
			def->SetAttribute(Player::HP, def->GetAttribute(Player::HP)-DMG);
			def->DmgAni();
		} else {
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			ophase=phase;
			phase=8;
			dly=90;
		}
	} else if (AT[atkn]==5) {
		if (rnd(100)<=Acc[atkn]-def->GetAttribute(Player::AVOIDANCE)) {
			int combo=rnd(4)+2;
			int AW=atk->GetAttribute(atkr)-def->GetAttribute(defr);
			int DMG=(int) ((float) AW*((float) Power[atkn]/100.0));
			if (DMG<=1) DMG=2;
			DMG=(int) ((float) DMG*(float) RW[AType[atkn]][def->GetType()]);
			DMG*=combo;
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			
			def->SetAttribute(Player::HP, def->GetAttribute(Player::HP)-DMG);
			def->DmgAni();
		} else {
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			ophase=phase;
			phase=8;
			dly=90;
		}
	} else if (AT[atkn]==1) {
		if (STAUS[atkn]==Player::ATKP && atk->GetAttribute(Player::ATKP)<5) {
			atk->SetAttribute(Player::ATKP, atk->GetAttribute(Player::ATKP)+1);
		} else if (STAUS[atkn]==Player::DEFP && atk->GetAttribute(Player::DEFP)<5) {
			atk->SetAttribute(Player::DEFP, atk->GetAttribute(Player::DEFP)+1);
		} else if (STAUS[atkn]==Player::RATKP && atk->GetAttribute(Player::RATKP)<5) {
			atk->SetAttribute(Player::RATKP, atk->GetAttribute(Player::RATKP)+1);
		} else if (STAUS[atkn]==Player::RDEFP && atk->GetAttribute(Player::RDEFP)<5) {
			atk->SetAttribute(Player::RDEFP, atk->GetAttribute(Player::RDEFP)+1);
		} else if (STAUS[atkn]==Player::SPDP && atk->GetAttribute(Player::SPDP)<20) {
			atk->SetAttribute(Player::SPDP, atk->GetAttribute(Player::SPDP)+5);
		} else if (STAUS[atkn]==Player::AVOIDANCE && atk->GetAttribute(Player::AVOIDANCE)<60) {
			atk->SetAttribute(Player::AVOIDANCE, atk->GetAttribute(Player::AVOIDANCE)+15);
		} else if (STAUS[atkn]==Player::HP && atk->GetAttribute(Player::HP)<atk->GetAttribute(Player::MAXHP)) {
			atk->SetAttribute(Player::HP, atk->GetAttribute(Player::HP)+(atk->GetAttribute(Player::MAXHP)/2));
		} else {
			ophase=phase;
			phase=11;
			dly=90;
		}
		
		if (phase!=11) atk->STAni(true);
		
		if (matk1>0) {
			if (pl==1) atk->MinAP(matk1);
			else if (pl==2) atk->MinAP(matk2);
		}
	} else if (AT[atkn]==2) {
		if (rnd(100)<=Acc[atkn]-def->GetAttribute(Player::AVOIDANCE)) {
			if (STAUS[atkn]==Player::ATKP && def->GetAttribute(Player::ATKP)>-5) {
				def->SetAttribute(Player::ATKP, def->GetAttribute(Player::ATKP)-1);
			} else if (STAUS[atkn]==Player::DEFP && def->GetAttribute(Player::DEFP)>-5) {
				def->SetAttribute(Player::DEFP, def->GetAttribute(Player::DEFP)-1);
			} else if (STAUS[atkn]==Player::RATKP && def->GetAttribute(Player::RATKP)>-5) {
				def->SetAttribute(Player::RATKP, def->GetAttribute(Player::RATKP)-1);
			} else if (STAUS[atkn]==Player::RDEFP && def->GetAttribute(Player::RDEFP)>-5) {
				def->SetAttribute(Player::RDEFP, def->GetAttribute(Player::RDEFP)-1);
			} else if (STAUS[atkn]==Player::SPDP && def->GetAttribute(Player::SPDP)>-15) {
				def->SetAttribute(Player::SPDP, def->GetAttribute(Player::SPDP)-4);
			} else if (STAUS[atkn]==Player::AVOIDANCE && def->GetAttribute(Player::AVOIDANCE)>-50) {
				def->SetAttribute(Player::AVOIDANCE, def->GetAttribute(Player::AVOIDANCE)-10);
			} else {
				ophase=phase;
				phase=11;
				dly=90;
			}
			
			if (phase!=11) def->STAni(false);
			
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
		} else {
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			ophase=phase;
			phase=8;
			dly=90;
		}
	} else if (AT[atkn]==7) {
		int AW=atk->GetAttribute(atkr)-def->GetAttribute(defr);
		int DMG=(int) ((float) AW*((float) Power[atkn]/100.0));
		if (DMG<=1) DMG=2;
		DMG=(int) ((float) DMG*(float) RW[AType[atkn]][def->GetType()]);
		if (matk1>0) {
			if (pl==1) atk->MinAP(matk1);
			else if (pl==2) atk->MinAP(matk2);
		}
		
		def->SetAttribute(Player::HP, def->GetAttribute(Player::HP)-DMG);
		def->DmgAni();
	} else if (AT[atkn]==9) {
		if (rnd(100)<=Acc[atkn]-def->GetAttribute(Player::AVOIDANCE)) {
			int AW=0;
			int DMG=(int) ((float) def->GetAttribute(Player::HP)/2.0);
			if (DMG<=1) DMG=2;
			DMG=(int) ((float) DMG*(float) RW[AType[atkn]][def->GetType()]);
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			
			def->SetAttribute(Player::HP, def->GetAttribute(Player::HP)-DMG);
			def->DmgAni(); 
		} else {
			if (matk1>0) {
				if (pl==1) atk->MinAP(matk1);
				else if (pl==2) atk->MinAP(matk2);
			}
			ophase=phase;
			phase=8;
			dly=90;
		}
	}
	
	if (STE[atkn]!=0 && phase!=8) {
		if (STE[atkn]==1 && rnd(100)<10 && def->GetSTP()==0) {def->STP(1, -1);}
		else if (STE[atkn]==2 && rnd(100)<15 && def->GetSTP()==0) {def->STP(2, -1);}
		else if (STE[atkn]==3 && rnd(100)<30 && def->GetSTP()==0) {if (pl==1 && firsta==1) {phase=17;ophase=7;dly=100;} else if (pl==2 && firsta==2) {phase=17;ophase=7;dly=100;}}
		else if (STE[atkn]==4 && rnd(100)<15 && def->GetSTP()==0) {def->STP(4, rnd(3)+1);}
		else if (STE[atkn]==5 && rnd(100)<10 && def->GetSTP()==0) {def->STP(5, rnd(3)+1);}
		else if (STE[atkn]==6 && rnd(100)<10 && def->GetSTP()==0) {def->STP(6, rnd(4)+1);}
	}
}

void ActionD::ActionAtk(Player *p1, Player *p2, int an1, int an2, int man1, int man2) {
	phase=1;dly=30;
	if (p1->GetSTP()==2) firsta=2;
	else if (p2->GetSTP()==2) firsta=1;
	else if (AT[an1]==3) firsta=1;
	else if (AT[an2]==3) firsta=2;
	else if (p1->GetAttribute(Player::SPEED)>p2->GetAttribute(Player::SPEED)) firsta=1;
	else if (p1->GetAttribute(Player::SPEED)<p2->GetAttribute(Player::SPEED)) firsta=2;
	else firsta=rnd(2)+1;
	
	tp1=p1;
	tp2=p2;
	atk1=an1;
	atk2=an2;
	matk1=man1;
	matk2=man2;
	p1rstp=false;
	p2rstp=false;
}

void ActionD::Update() {
	if (dly>0) dly--;
	
	if (phase==1 && dly<=0) {phase=2;dly=100;}
	if (phase==2 && dly<=0) {
		phase=3;dly=60;
		if (tp1->GetSTP()==2 && rnd(10)<4 && firsta==1) {phase=15;ophase=4;dly=100;}
		else if (tp2->GetSTP()==2 && rnd(10)<4 && firsta==2) {phase=16;ophase=4;dly=100;}
		else if (tp1->GetSTP()==4 && rnd(10)<4 && firsta==1) {phase=18;ophase=4;dly=100;}
		else if (tp2->GetSTP()==4 && rnd(10)<4 && firsta==2) {phase=19;ophase=4;dly=100;}
		else if (firsta==1 && atk1<100) {tp1->AtkAni(5+atk1);}
		else if (firsta==2 && atk2<100) {tp2->AtkAni(5+atk2);}
	}
	if (phase==3 && dly<=0) {
		phase=4;dly=60;
		if (firsta==1) {ActionDmg(tp1, tp2, atk1, 1);}
		else if (firsta==2) {ActionDmg(tp2, tp1, atk2, 2);}
	}
	if (phase==4 && dly<=0) {
		if (!tp1->GetLive()) {phase=9;dly=100;}
		else if (!tp2->GetLive()) {phase=10;dly=100;}
		else {phase=5;dly=100;}
	}
	if (phase==5 && dly<=0) {
		phase=6;dly=60;
		if (tp1->GetSTP()==2 && rnd(10)<4 && firsta==2) {phase=15;ophase=7;dly=100;}
		else if (tp2->GetSTP()==2 && rnd(10)<4 && firsta==1) {phase=16;ophase=7;dly=100;}
		else if (tp1->GetSTP()==4 && rnd(10)<4 && firsta==2) {phase=18;ophase=7;dly=100;}
		else if (tp2->GetSTP()==4 && rnd(10)<4 && firsta==1) {phase=19;ophase=7;dly=100;}
		else if (firsta==2 && atk1<100) {tp1->AtkAni(5+atk1);}
		else if (firsta==1 && atk2<100) {tp2->AtkAni(5+atk2);}
	}
	if (phase==6 && dly<=0) {
		phase=7;dly=100;
		if (firsta==2) {ActionDmg(tp1, tp2, atk1, 1);}
		else if (firsta==1) {ActionDmg(tp2, tp1, atk2, 2);}
	}
	if (phase==7 && dly<=0) {
		if (tp1->GetSTP()==1 && !p1rstp) {phase=12;dly=100;ophase=7;p1rstp=true;}
		else if (tp2->GetSTP()==1 && !p2rstp) {phase=13;dly=100;ophase=7;p2rstp=true;}
		else if (!tp1->GetLive() && !tp2->GetLive()) {phase=14;dly=100;}
		else if (!tp1->GetLive()) {phase=9;dly=100;}
		else if (!tp2->GetLive()) {phase=10;dly=100;}
		else {actend=true;phase=0;if (tp1->GetSTP()!=0) tp1->STPAC();if (tp2->GetSTP()!=0) tp2->STPAC();}
	}
	if (phase==9) {tp1->SetAni(2);}
	if (phase==10) {tp2->SetAni(2);}
	if (phase==14) {tp1->SetAni(2);tp2->SetAni(2);}
	
	if (phase==8 && dly<=0) {phase=ophase;dly=60;}
	if (phase==11 && dly<=0) {phase=ophase;dly=60;}
	if (phase==12 && dly<=0) {phase=ophase;dly=30;tp1->STPA();}
	if (phase==13 && dly<=0) {phase=ophase;dly=30;tp2->STPA();}
	if (phase==15 && dly<=0) {phase=ophase;dly=60;}
	if (phase==16 && dly<=0) {phase=ophase;dly=60;}
	if (phase==17 && dly<=0) {phase=ophase;dly=30;}
	if (phase==18 && dly<=0) {phase=ophase;dly=60;tp1->STPC();}
	if (phase==19 && dly<=0) {phase=ophase;dly=60;tp2->STPC();}
	
	if (phase==12 && dly==80) {tp1->AtkAni(105);}
	if (phase==13 && dly==80) {tp2->AtkAni(105);}
	if (phase==15 && dly==80) {tp1->AtkAni(106);}
	if (phase==16 && dly==80) {tp2->AtkAni(106);}
	
	//Phase 1-6 Ani, 7 AniCheckEnd, 8 doesn't hit, 9 P1 Defeat, 10 P2 Defeat, 11 dont work, 12 P1 Burn, 13 P2 Burn
	//Phase2 14 Draw, 15 P1 Par, 16 P2 Par, 17 Shock, 18 P1 Confuse, 19 P2 Confuse
}

Bar::Bar(BITMAP *img, Player *targ, int x, int y, int w, int h):BaseObject(img, x, y, w, h) {
	this->Target=targ;
	this->value=(float) GetW()-3.0;
	this->valuev=(float) GetW()-3.0;
}

void Bar::Update() {
	valuev=((float) Target->GetAttribute(Player::HP)/Target->GetAttribute(Player::MAXHP))*((float) GetW()-3.0);
	if ((int) valuev<value) {
		value-=1.0;
	} else if ((int) valuev>value) {
		value+=1.0;
	}
	if (value<7.0 && valuev>=3.0) value=7.0;
}

void Bar::Show() {
	if (value<32) rectfill(doubleBuffer, x, y, x+((int) value), y+h-2, makecol(200, 50, 50));
	else if (value<100) rectfill(doubleBuffer, x, y, x+((int) value), y+h-2, makecol(240, 240, 0));
	else rectfill(doubleBuffer, x, y, x+((int) value), y+h-2, makecol(0, 200, 50));
	if(Pic) draw_sprite(doubleBuffer, Pic, x, y);
}

AtkMBtn::AtkMBtn(BITMAP *img, Player *targ, int x, int y, int w, int h, int anum, string atkn, int maxap, int mn):BaseObject(img, x, y, w, h) {
	this->Target=targ;
	
	this->AtkNum=anum;
	this->MAP=maxap;
	this->AP=MAP;
	this->MNum=mn;
	
	AtkName=new char[16];
	this->AtkName=atkn;
}

void AtkMBtn::Update() {
	AP=Target->GetAP(MNum);
}

void AtkMBtn::Show() {
	if(Pic) draw_sprite(doubleBuffer, Pic, x, y);
	textprintf_centre_ex(doubleBuffer, font, x+60, y+20, makecol(0, 0, 0), -1, "%s", AtkName.c_str());
	textprintf_centre_ex(doubleBuffer, font, x+60, y+32, makecol(0, 0, 0), -1, "%d / %d", AP, MAP);
}

MenuSelect::MenuSelect(BITMAP *img, int x, int y, int w, int h, AtkMBtn *a1, AtkMBtn *a2, AtkMBtn *a3, AtkMBtn *a4):BaseObject(img, x, y, w, h) {
	this->pa1=a1;
	this->pa2=a2;
	this->pa3=a3;
	this->pa4=a4;
	
	this->selected=1;
	this->focused=true;
}

int MenuSelect::AtkSelected() {
	if (selected==1) return pa1->GetAtkN();
	else if (selected==2) return pa2->GetAtkN();
	else if (selected==3) return pa3->GetAtkN();
	else if (selected==4) return pa4->GetAtkN();
}

void MenuSelect::Selects(int value) {
	if (value==DUP && Selected()>=3) {selected-=2;}
	else if (value==DRIGHT) {if (Selected()==1 || Selected()==3) selected+=1;}
	else if (value==DDOWN && Selected()<=2) {selected+=2;}
	else if (value==DLEFT) {if (Selected()==2 || Selected()==4) selected-=1;}
}

void MenuSelect::Update() {
	pa1->Update();
	pa2->Update();
	pa3->Update();
	pa4->Update();
}

void MenuSelect::Show() {
	pa1->Show();
	pa2->Show();
	pa3->Show();
	pa4->Show();
	if (selected==1) rect(doubleBuffer, pa1->GetX(), pa1->GetY(), pa1->GetX()+120, pa1->GetY()+60, makecol(128, 0, 0));
	else if (selected==2) rect(doubleBuffer, pa2->GetX(), pa2->GetY(), pa2->GetX()+120, pa2->GetY()+60, makecol(128, 0, 0));
	else if (selected==3) rect(doubleBuffer, pa3->GetX(), pa3->GetY(), pa3->GetX()+120, pa3->GetY()+60, makecol(128, 0, 0));
	else if (selected==4) rect(doubleBuffer, pa4->GetX(), pa4->GetY(), pa4->GetX()+120, pa4->GetY()+60, makecol(128, 0, 0));
}

BattleMain::~BattleMain() {
   	delete PL1;
   	delete PL2;
   	delete HPB1;
   	delete HPB2;
   	delete AMB1;
	delete AMB2;
	delete AMB3;
	delete AMB4;
	delete MS;
	delete AD;
	
	destroy_bitmap(Character1_P1);
	destroy_bitmap(Character1_P2);
	destroy_bitmap(Menu);
	destroy_bitmap(AtkM);
	destroy_bitmap(UpSt);
	destroy_bitmap(DnSt);
	destroy_bitmap(BarSquare);
	destroy_bitmap(BarBorder);
	destroy_bitmap(BG);
	destroy_bitmap(Ground);
}
        
void BattleMain::BattleInit(const char* plc) {
   	joypad=new Joypad();
	
	//Loading Bitmaps
	Character1_P1=load_bitmap("bitmaps/ch1_p1.tga", NULL);
	Character1_P2=load_bitmap("bitmaps/ch1_p2.tga", NULL);
	
	Menu=load_bitmap("bitmaps/menu.tga", NULL);
	AtkM=load_bitmap("bitmaps/atkmenu.tga", NULL);
	UpSt=load_bitmap("bitmaps/upstatus.tga", NULL);
	DnSt=load_bitmap("bitmaps/dnstatus.tga", NULL);
	BarSquare=load_bitmap("bitmaps/barsquare.tga", NULL);
	BarBorder=load_bitmap("bitmaps/bar.tga", NULL);
	BG=load_bitmap("bitmaps/forest.tga", NULL);
	Ground=load_bitmap("bitmaps/ground_city.tga", NULL);
	
	//Loading Samples
	Bgm=load_sample("sounds/bgm.wav");
        	
	//Loading Classes
	AD=new ActionD(AtkM, 0, 0, 1, 1);
	PL1=new Player(Character1_P1, UpSt, DnSt, 50, 220, 200, 200, plc, 1);
	PL2=new Player(Character1_P2, UpSt, DnSt, 390, 90, 120, 120, "botdata/listectrix.tbd", 2);
	HPB1=new Bar(BarBorder, PL1, 100, 75, 200, 10);
	HPB2=new Bar(BarBorder, PL2, 350, 275, 200, 10);
	AMB1=new AtkMBtn(AtkM, PL1, 150, 340, 120, 60, PL1->GetAtk(1), AD->AtkNames(PL1->GetAtk(1)), AD->MaxAP(PL1->GetAtk(1)), 1);
	AMB2=new AtkMBtn(AtkM, PL1, 300, 340, 120, 60, PL1->GetAtk(2), AD->AtkNames(PL1->GetAtk(2)), AD->MaxAP(PL1->GetAtk(2)), 2);
	AMB3=new AtkMBtn(AtkM, PL1, 150, 410, 120, 60, PL1->GetAtk(3), AD->AtkNames(PL1->GetAtk(3)), AD->MaxAP(PL1->GetAtk(3)), 3);
	AMB4=new AtkMBtn(AtkM, PL1, 300, 410, 120, 60, PL1->GetAtk(4), AD->AtkNames(PL1->GetAtk(4)), AD->MaxAP(PL1->GetAtk(4)), 4);
	MS=new MenuSelect(AtkM, 150, 340, 120, 60, AMB1, AMB2, AMB3, AMB4);
				
	//Loading Set
	PL1->SetAP(1, AD->MaxAP(PL1->GetAtk(1)));
	PL1->SetAP(2, AD->MaxAP(PL1->GetAtk(2)));
	PL1->SetAP(3, AD->MaxAP(PL1->GetAtk(3)));
	PL1->SetAP(4, AD->MaxAP(PL1->GetAtk(4)));
	
	PL2->SetAP(1, AD->MaxAP(PL2->GetAtk(1)));
	PL2->SetAP(2, AD->MaxAP(PL2->GetAtk(2)));
	PL2->SetAP(3, AD->MaxAP(PL2->GetAtk(3)));
	PL2->SetAP(4, AD->MaxAP(PL2->GetAtk(4)));
				
	//Loading Variables
	gameOver=false;
	needsRedraw=false;
	
	turn=1;
	sdelay=0;
	adelay=0;
	
	PL1TA=1;
	PL2TA=1;
	PL1LA=1;
	PL2LA=1;
	
	firstturn=1;
	
	TmL=100;
}
        
void BattleMain::Bars(int x, int y, Bar *br, int pl) {
	draw_sprite(doubleBuffer, BarSquare, x, y);
	if (br->GetX()!=x+50 || br->GetY()!=y+25) br->SetPos(x+50, y+25);
	br->Show();
	textout_ex(doubleBuffer, font, "HP", x+30, y+26, makecol(0, 128, 32), -1);
	if (pl==1) {
		textprintf_ex(doubleBuffer, font, x+100, y+40, makecol(0, 0, 0), -1, "%d / %d", PL1->GetAttribute(Player::HP), PL1->GetAttribute(Player::MAXHP));
		textprintf_ex(doubleBuffer, font, x+40, y+10, makecol(0, 0, 0), -1, "%s", PL1->CallName());
		textprintf_ex(doubleBuffer, font, x+200, y+10, makecol(0, 0, 0), -1, "Lv. %d", PL1->GetAttribute(Player::LV));
		
		if (PL1->GetSTP()==1) textout_ex(doubleBuffer, font, "BURN", x+50, y+40, makecol(200, 20, 50), -1);
		if (PL1->GetSTP()==2) textout_ex(doubleBuffer, font, "PAR", x+50, y+40, makecol(230, 230, 20), -1);
		if (PL1->GetSTP()==4) textout_ex(doubleBuffer, font, "CON", x+50, y+40, makecol(128, 0, 128), -1);
	} else {
		textprintf_ex(doubleBuffer, font, x+40, y+10, makecol(0, 0, 0), -1, "%s", PL2->CallName());
		textprintf_ex(doubleBuffer, font, x+200, y+10, makecol(0, 0, 0), -1, "Lv. %d", PL2->GetAttribute(Player::LV));
		
		if (PL2->GetSTP()==1) textout_ex(doubleBuffer, font, "BURN", x+50, y+40, makecol(200, 20, 50), -1);
		if (PL2->GetSTP()==2) textout_ex(doubleBuffer, font, "PAR", x+50, y+40, makecol(230, 230, 20), -1);
		if (PL2->GetSTP()==4) textout_ex(doubleBuffer, font, "CON", x+50, y+40, makecol(128, 0, 128), -1);
	}
}
              
void BattleMain::MainLoop() {
	play_sample(Bgm, 100, 128, 1000, 1);
	while (!gameOver) {
		if (timerCounter) {
			
			joypad->poll();
			if (joypad->button[Joypad::UP] && TmL<=0) {
				if (sdelay<=0) {MS->Selects(DUP);sdelay=15;}
			} else if (joypad->button[Joypad::DOWN] && TmL<=0) {
				if (sdelay<=0) {MS->Selects(DDOWN);sdelay=15;}
			} else if (joypad->button[Joypad::RIGHT] && TmL<=0) {
				if (sdelay<=0) {MS->Selects(DRIGHT);sdelay=15;}
			} else if (joypad->button[Joypad::LEFT] && TmL<=0) {
				if (sdelay<=0) {MS->Selects(DLEFT);sdelay=15;}
			} else if (joypad->button[Joypad::ACTION] || joypad->button[Joypad::USE]) {
				if (sdelay<=0 && turn==1 && PL1->GetAP(MS->Selected())>0 && TmL<=0) {PL1TA=MS->AtkSelected();PL1LA=MS->Selected();sdelay=15;turn=2;}
			} else {
			}
			
			// Others
			
				if (TmL>0) TmL--;
			
			// Künstliche Intelligents
			
			if (turn==2) {
				if (!Nnr2) { 
					if (PL2->GetAttribute(Player::LV)>=5) {
						if (AD->GetStaus(PL2->GetAtk(1))==Player::HP && PL2->GetAttribute(Player::HP)<(PL2->GetAttribute(Player::MAXHP)/2) && rnd(2)==1 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(1);
							PL2LA=1;
						} else if (AD->GetStaus(PL2->GetAtk(2))==Player::HP && PL2->GetAttribute(Player::HP)<(PL2->GetAttribute(Player::MAXHP)/2) && rnd(2)==1 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(2);
							PL2LA=2;
						} else if (AD->GetStaus(PL2->GetAtk(3))==Player::HP && PL2->GetAttribute(Player::HP)<(PL2->GetAttribute(Player::MAXHP)/2) && rnd(2)==1 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(3);
							PL2LA=3;
						} else if (AD->GetStaus(PL2->GetAtk(4))==Player::HP && PL2->GetAttribute(Player::HP)<(PL2->GetAttribute(Player::MAXHP)/2) && rnd(2)==1 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(4);
							PL2LA=4;
						}
						
						else if (AD->Effective(AD->AtkType(PL2->GetAtk(1)), PL1->GetAttribute(Player::TYPE))>1.0 && rnd(2)==1 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(1);
							PL2LA=1;
						} else if (AD->Effective(AD->AtkType(PL2->GetAtk(2)), PL1->GetAttribute(Player::TYPE))>1.0 && rnd(2)==1 && PL2->GetAP(2)>0) {
							PL2TA=PL2->GetAtk(2);
							PL2LA=2;
						} else if (AD->Effective(AD->AtkType(PL2->GetAtk(3)), PL1->GetAttribute(Player::TYPE))>1.0 && rnd(2)==1 && PL2->GetAP(3)>0) {
							PL2TA=PL2->GetAtk(3);
							PL2LA=3;
						} else if (AD->Effective(AD->AtkType(PL2->GetAtk(4)), PL1->GetAttribute(Player::TYPE))>1.0 && rnd(2)==1 && PL2->GetAP(4)>0) {
							PL2TA=PL2->GetAtk(4);
							PL2LA=4;
						}
						
						else if (rnd(6)==3 && PL2->GetAP(1)>0) {
							PL2TA=PL2->GetAtk(1);
							PL2LA=1;
						}
						
						else {	
							int airnd=rnd(4)+1;
							if (PL2->GetAP(airnd)>0) {
								PL2TA=PL2->GetAtk(airnd);
								PL2LA=airnd;
						} else {
								PL2TA=100;
								PL2LA=0;
								Nnr2=true;
							}
						}
					} else {
						int airnd=rnd(4)+1;
						PL2TA=PL2->GetAtk(airnd);
						PL2LA=airnd;
					}
				}
				
				turn=0;
				AD->ActionAtk(PL1, PL2, PL1TA, PL2TA, PL1LA, PL2LA);
			}
			
			// End KI
			
			if (AD->TES() && turn==0) {
				Nnr1=Nnr2=false;
				if (AD->NNR()==3) {Nnr1=true;Nnr2=true;turn=2;PL1TA=100;PL1LA=0;PL2TA=100;PL2LA=0;}
				else if (AD->NNR()==1) {Nnr1=true;turn=2;PL1TA=100;PL1LA=0;}
				else if (AD->NNR()==2) {Nnr2=true;turn=1;PL2TA=100;PL2LA=0;}
				else {turn=1;}
				AD->OTS();
			}
			
			if (sdelay>0 && turn==1) sdelay--;
			
			if (key[KEY_ESC]) gameOver=true;
			
			do {
				PL1->Update();
				PL2->Update();
				HPB1->Update();
				HPB2->Update();
				MS->Update();
				AD->Update();
                                 
				--timerCounter;
		} while (timerCounter>0);
                     needsRedraw=true;
		}
             
		if (needsRedraw) {
			clear(doubleBuffer);
			blit(BG, doubleBuffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
			stretch_sprite(doubleBuffer, Ground, -50, 300, 400, 100);
			draw_sprite(doubleBuffer, Ground, 350, 170);
			if (turn==0 && AD->Phase()==3 && AD->FTurn()==1) {
				PL2->Show();
				PL1->Show();
			} else if (turn==0 && AD->Phase()==3 && AD->FTurn()==2) {
				PL1->Show();
				PL2->Show();
			} else if (turn==0 && AD->Phase()==6 && AD->FTurn()==1) {
				PL1->Show();
				PL2->Show();
			} else if (turn==0 && AD->Phase()==6 && AD->FTurn()==2) {
				PL2->Show();
				PL1->Show();
			} else {
				PL2->Show();
				PL1->Show();
			}
                  
            if (AD->Phase()!=3 && AD->Phase()!=6 && AD->Phase()!=12 && AD->Phase()!=13 && AD->Phase()!=15 && AD->Phase()!=16) {
				Bars(50, 50, HPB2, 2);
				Bars(300, 225, HPB1, 1);
            } else if (Nnr1 || Nnr2) {
               	Bars(50, 50, HPB2, 2);
				Bars(300, 225, HPB1, 1);
            }
                  
            draw_sprite(doubleBuffer, Menu, 0, 330);
            if (turn==1) MS->Show();
			if (turn==0 && AD->Phase()==2) {
				if (AD->FTurn()==1) {
					if (!Nnr1) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s uses %s", PL1->CallName(), AD->AtkNames(PL1TA).c_str());
					else textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s must skip this round", PL1->CallName());
				}
				else if (AD->FTurn()==2) {
					if (!Nnr2) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s (Enemy) uses %s", PL2->CallName(), AD->AtkNames(PL2TA).c_str());
					else textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s must skip this round", PL2->CallName());
				}
			}
			else if (turn==0 && AD->Phase()==5) {
				if (AD->FTurn()==2) {
					if (!Nnr1) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s uses %s", PL1->CallName(), AD->AtkNames(PL1TA).c_str());
					else textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s must skip this round", PL1->CallName());
				}
				else if (AD->FTurn()==1) {
					if (!Nnr2) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s (Enemy) uses %s", PL2->CallName(), AD->AtkNames(PL2TA).c_str());
					else textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s must skip this round", PL2->CallName());
				}
            }
            else if (turn==0 && AD->Phase()==8) {
               	/*if (AD->FTurn()==2) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s use %s", PL1->CallName(), AD->AtkNames(PL1TA).c_str());
				else if (AD->FTurn()==1) textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s (Enemy) use %s", PL2->CallName(), AD->AtkNames(PL2TA).c_str());*/
				textout_ex(doubleBuffer, font, "It doesn't hit", 50, 400, makecol(0, 0, 0), -1);
            }
            else if (turn==0 && AD->Phase()==9) {
               	textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is defeated", PL1->CallName());
            }
			else if (turn==0 && AD->Phase()==10) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is defeated", PL2->CallName());
			} 
			else if (turn==0 && AD->Phase()==14) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s and %s are defeated", PL1->CallName(), PL2->CallName());
			}
			else if (turn==0 && AD->Phase()==11) {
				textout_ex(doubleBuffer, font, "It doesn't work", 50, 400, makecol(0, 0, 0), -1);
			}
			else if (turn==0 && AD->Phase()==12) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "The burn damages %s", PL1->CallName());
			}
			else if (turn==0 && AD->Phase()==13) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "The burn damages %s", PL2->CallName());
			}
			else if (turn==0 && AD->Phase()==15) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is paralyzed", PL1->CallName());
			}
			else if (turn==0 && AD->Phase()==16) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is paralyzed", PL2->CallName());
			}
			else if (turn==0 && AD->Phase()==17) {
				if (AD->FTurn()==1) {
					textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is shocked", PL2->CallName());
				} else if (AD->FTurn()==2) {
					textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is shocked", PL1->CallName());
				}
			}
			else if (turn==0 && AD->Phase()==18) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is confused. The confuse damages %s", PL1->CallName(), PL1->CallName());
			}
			else if (turn==0 && AD->Phase()==19) {
				textprintf_ex(doubleBuffer, font, 50, 400, makecol(0, 0, 0), -1, "%s is confused. The confuse damages %s", PL2->CallName(), PL2->CallName());
			}
			
			needsRedraw=false;
			show();
		}
	}
	stop_sample(Bgm);
}
